#include "Cpersuit.h"
#include <Cmaid.h>
#include <Ecore_Data.h>
#include <Eina.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>

void
cmaid_set_test(unsigned int run, Cps_Stopwatch watches[], Cmaid_Set *set)
{
	char buffer[254];
	unsigned int i;
	const char *text;
	Cmaid_Iter iter;
	size_t len = 0;

	/* add */
	cps_stopwatch_start(&watches[0]);
	for (i = 0; i < run; i++)
	{
		snprintf(buffer, sizeof(buffer), "text%dbla", i);
		cmaid_set_add(set, buffer);
	}
	cps_stopwatch_stop(&watches[0]);

	/* iterate */
	cps_stopwatch_start(&watches[1]);
	cmaid_container_iter_attach(set, &iter);
	while ((text = cmaid_iter_next(&iter)))
	/* do something */
		len += strlen(text);
	cmaid_container_iter_detach(set, &iter);
	cps_stopwatch_stop(&watches[1]);

	/* find */
	cps_stopwatch_start(&watches[2]);
	for (i = 0; i < run/2; i++)
	{
		snprintf(buffer, sizeof(buffer), "text%dbla", i);
		cmaid_set_get(set, buffer);
		snprintf(buffer, sizeof(buffer), "text%dbla", run - i);
		cmaid_set_get(set, buffer);
	}
	cps_stopwatch_stop(&watches[2]);
	
	/* remove */
	cps_stopwatch_start(&watches[3]);
	for (i = 0; i < run; i++)
	{
		snprintf(buffer, sizeof(buffer), "text%dbla", i);
		cmaid_set_remove(set, buffer);
	}
	cps_stopwatch_stop(&watches[3]);

	/* fill it again */
	for (i = 0; i < run; i++)
	{
		snprintf(buffer, sizeof(buffer), "text%dbla", i);
		cmaid_set_add(set, buffer);
	}

	/* clear */
	cps_stopwatch_start(&watches[4]);
	cmaid_container_clear(set);
	cps_stopwatch_stop(&watches[4]);
}

void
cmaid_set_test_hash(unsigned int run, Cps_Stopwatch watches[])
{
	Cmaid_List *set = cmaid_hash_set_new(&cmaid_value_inline_string);
	cmaid_set_test(run, watches, set);
	cmaid_container_destroy(set);
}

void
cmaid_set_test_tree(unsigned int run, Cps_Stopwatch watches[])
{
	Cmaid_List *set = cmaid_tree_set_new(&cmaid_value_inline_string);
	cmaid_set_test(run, watches, set);
	cmaid_container_destroy(set);
}

void
cmaid_set_test_skip_list(unsigned int run, Cps_Stopwatch watches[])
{
	Cmaid_List *set = cmaid_skip_list_set_new(4,
			&cmaid_value_inline_string);
	cmaid_set_test(run, watches, set);
	cmaid_container_destroy(set);
}

#define START 1
#define STOP  1000
#define STEP  20

CPS_RUN_LIST
	CPS_RUN("Cmaid", "Hash", cmaid_set_test_hash,
			START, STOP, STEP,
		       	CPS_TEST("Add", CPS_O_LINEAR),
			CPS_TEST("Iterate", CPS_O_LINEAR),
			CPS_TEST("Find", CPS_O_LINEAR),
			CPS_TEST("Remove", CPS_O_LINEAR),
			CPS_TEST("Clear", CPS_O_LINEAR)
		),
	CPS_RUN("Cmaid", "Tree", cmaid_set_test_tree,
			START, STOP, STEP,
		       	CPS_TEST("Add", CPS_O_NLOG),
			CPS_TEST("Iterate", CPS_O_LINEAR),
			CPS_TEST("Find", CPS_O_NLOG),
			CPS_TEST("Remove", CPS_O_NLOG),
			CPS_TEST("Clear", CPS_O_LINEAR)
		),
	CPS_RUN("Cmaid", "Skip List", cmaid_set_test_skip_list,
			START, STOP, STEP,
		       	CPS_TEST("Add", CPS_O_NLOG),
			CPS_TEST("Iterate", CPS_O_LINEAR),
			CPS_TEST("Find", CPS_O_NLOG),
			CPS_TEST("Remove", CPS_O_NLOG),
			CPS_TEST("Clear", CPS_O_LINEAR)
		)
CPS_RUN_LIST_END

